package com.text.controller;

import com.text.entity.Adminuser;
import com.text.service.AdminuserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;

@Controller
@RequestMapping("/admin")
public class LoginCtrl {

    @Resource
    AdminuserService adminuserService;

    @RequestMapping("/toLogin")
    public String gologin(){
        return "login";
    }

    @RequestMapping("/doLogin")
    public String doLogin(Adminuser adminuser, Model model, HttpSession session){
        System.out.println(adminuser.getName() + "====" + adminuser.getPassword());
        Adminuser user =  adminuserService.getAdminuser(adminuser);

        if(user == null){
            model.addAttribute("msg","用户名或者密码不正确");
            return "login";
        }
        session.setAttribute("aduUserName",user.getName());
        return "redirect:/admin/goodsList";
    }
    @GetMapping("/logout")
    public String doLogout(HttpSession session, SessionStatus sessionStatus){
        // 只是让session失效 但session仍然存在
        session.invalidate();
        sessionStatus.setComplete();
        return "login";
    }


}
